package com.easymin.weixin.common.scheduler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.io.File;
import java.io.IOException;

@Component
public class ClearImage {
  
  private static final Logger logger = LoggerFactory.getLogger(ClearImage.class);
  
  /**
   * 定期清理图片.
   */
  
  @Transactional(rollbackFor = Exception.class)
  public void clearImgs() throws IOException {
    try {
      String path = System.getProperty("user.dir");
      path += "/src/main/webapp/upload/errand";
      File file = new File(path);
      deleteDir(file);
    } catch (Exception ex) {
      logger.error(ex.getMessage());
    }
  }
  /**
   * 是否成功删除图片.
   */
  
  public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
      String[] children = dir.list();
      if (null != children) {
        for (int i = 0; i < children.length; i++) {
          File file = new File(dir, children[i]);
          boolean success = file.delete();
          if (!success) {
            return false;
          }
        }
      }
      return true;
    }
    return false;
  }
}
